package com.fanbai.score.user.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.common.Manager.ActivityManager;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.base.MyApplication;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.user.bean.CartGoodsInfo_Bean;
import com.fanbai.score.user.http.UserOrder_RequestNet_Utils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by acer on 2017/6/3.
 */

public class ShopCart_Activity extends BaseActivity{

    @BindView(R.id.view_recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.userActivityShopCart_totalPrice)
    TextView totalPrice;

    @OnClick(R.id.userActivityShopCart_submitBut)
    public void submitBut(){
//        jumpToActivity(CartOrder_Activity.class);
//        ToastUtils.WarnImageToast(ctx, opinfos.toString());
        submitproductexchange(opinfos.toString());
    }

    @Override
    protected int getLayoutID() {
        return R.layout.user_activity_shopcart;
    }

    @Override
    public String getTitleName() {
        return "购物车";
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        List<CartGoodsInfo_Bean> beans = MyApplication.getInstanti().getUserCartGoods();
        totalPrice.setText("总积分：" + calculationAllPrice(beans));
//        List<String> dataList = new ArrayList<>();
//        dataList.add(".png");
//        dataList.add("3&dl.jpg");
//        dataList.add("h&.jpg");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(ctx));
        mRecyclerView.setAdapter(new CommonAdapter<CartGoodsInfo_Bean>(ctx, R.layout.user_item_shopcart,
                beans){
            @Override
            protected void convert(ViewHolder holder, CartGoodsInfo_Bean bean, int position) {
                ImageUtils
                        .show(ctx, bean.getGoodsImg(), (ImageView) holder.getView(R.id.userItemShopCart_shopImg), R.mipmap.icon_img_defeated);
                holder.setText(R.id.userItemShopCart_shopName, bean.getGoodsName());
                holder.setText(R.id.userItemShopCart_shopPirce, ""+bean.getGoodsPrice());
                holder.setText(R.id.userItemShopCart_shopCnt, ""+bean.getGoodsCnt());
            }
        });
    }

    private StringBuffer opinfos = new StringBuffer();
    private int calculationAllPrice(List<CartGoodsInfo_Bean> beans){
        int price = 0;
        opinfos.append("[");
        CartGoodsInfo_Bean bean ;
        for(int i = 0; i < beans.size(); i++){
            bean = beans.get(i);
            price += (bean.getGoodsPrice() * bean.getGoodsCnt());
            opinfos.append(bean.getGoodsPid()+":"+bean.getGoodsCnt());
            if(i < beans.size()-1){
                opinfos.append(",");
            }
        }
        opinfos.append("]");
        return price;
    }

    @Override
    protected void initListener() {

    }

    @Override
    public int getTitleLeftName() {
        return R.mipmap.back_click;
    }

    @Override
    public void onClickTitleLeft() {
        ActivityManager.getInstance().finishActivity();
    }

    private void submitproductexchange(String opinfos){
        showDialog();
        UserOrder_RequestNet_Utils.submitproductexchange(ctx, opinfos, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
                ActivityManager.getInstance().finishActivity();
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }
        });
    }
}
